package cn.ylz.ylmall.content.controller;


import cn.ylz.ylmall.account.pojo.vo.PageData;
import cn.ylz.ylmall.account.security.CurrentPrincipal;
import cn.ylz.ylmall.common.web.JsonResult;
import cn.ylz.ylmall.content.pojo.param.ContentAddNewParam;
import cn.ylz.ylmall.content.pojo.vo.ContentListItemVO;
import cn.ylz.ylmall.content.pojo.vo.ContentStandardVO;
import cn.ylz.ylmall.content.pojo.vo.CountVO;
import cn.ylz.ylmall.content.service.ICollectService;
import cn.ylz.ylmall.content.service.ICommentService;
import cn.ylz.ylmall.content.service.IContentService;
import cn.ylz.ylmall.content.service.ILikeService;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

/**
 * 圈子模块
 */
@Slf4j
@RestController
@RequestMapping("/v1/contents")
@Validated
@Api(tags = "2.1. 圈子内容模块")

public class ContentController {

    @Autowired
    private IContentService contentService;
    @Autowired
    private ICommentService commentService;
    @Autowired
    private ILikeService likeService;
    @Autowired
    private ICollectService collectService;

    /**
     * 发布内容
     * @param currentPrincipal
     * @param request
     * @param contentAddNewParam
     * @return
     */
    @PostMapping("/post-content")
    @ApiOperation("发布内容")
    @ApiOperationSupport(order = 101)
    public JsonResult postContent(@AuthenticationPrincipal @ApiIgnore CurrentPrincipal currentPrincipal,
                                  @ApiIgnore HttpServletRequest request,
                                  @Valid ContentAddNewParam contentAddNewParam) {
        log.debug("开始处理【发布内容】的请求，参数：{}", contentAddNewParam);
        String remoteAddr = request.getRemoteAddr();
        contentService.addNew(currentPrincipal, remoteAddr, contentAddNewParam);
        return JsonResult.ok();
    }

    /**
     * 按照内容id删除
     * @param id
     * @return
     */
    @PostMapping("/{id:[0-9]+}/delete")
  //  @PreAuthorize("hasAuthority('/content/delete-content')")
    @ApiOperation("根据id删除内容")
    @ApiOperationSupport(order = 102)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "内容id", required = true, paramType = "path")
    })
    public JsonResult delete(@PathVariable @Range(min = 1, message = "请提交有效的文章ID值！") Long id) {
        log.debug("开始处理【根据ID删除文章】的请求，参数：{}", id);
        contentService.delete(id);
        return JsonResult.ok();
    }

    /**
     * 根据id查询内容
     * @param id
     * @return
     */
    @GetMapping("/{id:[0-9]+}/detail")
   // @PreAuthorize("hasAuthority('/content/content-detail')")
    @ApiOperation("根据id获取内容详情")
    @ApiOperationSupport(order = 103)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "内容id", required = true, paramType = "path")
    })

    public JsonResult getStandardById(@PathVariable @Range(min = 1, message = "请提交有效的文章ID值！") Long id) {
        log.debug("开始处理【根据ID获取文章详情】的请求，参数：{}", id);
        ContentStandardVO queryResult = contentService.getStandardById(id);
        //统计点赞,收藏,评论数
        CountVO likeCount = likeService.count(id);
        CountVO commentCount = commentService.countByContentId(id);
        CountVO collectCount = collectService.count(id);
        contentService.update(id,likeCount.getCount(),commentCount.getCount(),collectCount.getCount());
        return JsonResult.ok(queryResult);
    }

    /**
     * 圈子首页内容列表
     * @param page
     * @return
     */
    @GetMapping("/list")
    //@PreAuthorize("hasAuthority('/content/content-list')")
    @ApiOperation("获取内容列表")
    @ApiOperationSupport(order = 100)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "页码", defaultValue = "1", paramType = "query",dataType = "long"),
    })
   public JsonResult list(@Range(min = 1, message = "请提交有效的文章页码值！") Integer page) {
       log.debug("开始处理【获取文章列表】的请求,页码:{}", page);
       Integer pageNum = page == null ? 1 : page;
       PageData<ContentListItemVO> pageData = contentService.list(pageNum);
       return JsonResult.ok(pageData);
   }

    /**
     * 按分类查询
     * @param categoryId
     * @param page
     * @return
     */
   @GetMapping("/list-by-category")
   //@PreAuthorize("hasAuthority('/content/content-list')")
   @ApiOperation("根据分类获取内容列表")
   @ApiOperationSupport(order = 104)
   @ApiImplicitParams({
           @ApiImplicitParam(name = "categoryId", value = "文章类别ID", required = true, paramType = "query", dataType = "long"),
           @ApiImplicitParam(name = "page", value = "页码", defaultValue = "1", paramType = "query", dataType = "long")
   })
    public JsonResult listByCategoryId(@Range(min = 1, message = "请提交有效的文章页码值！") Long categoryId,
                                      @Range(min = 1, message = "请提交有效的文章页码值！") Integer page)
   {
       log.debug("开始处理【根据分类获取文章列表】的请求,页码:{}", page);
       Integer pageNum = page == null ? 1 : page;
       PageData<ContentListItemVO> pageData = contentService.listByCategoryId(categoryId, pageNum);
       return JsonResult.ok(pageData);
   }

    @PostMapping("/{id:[0-9]+}/update")
    @ApiOperation("修改内容")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "内容id", required = true, paramType = "path")
    })
    public JsonResult updateContent(@PathVariable @Range(min = 1, message = "请提交有效的文章ID值！") Long id,
                            @Valid ContentAddNewParam contentAddNewParam) {
       log.debug("开始处理【修改内容】的请求，参数：{}",contentAddNewParam);
       contentService.updateContent(id,contentAddNewParam);
       return JsonResult.ok("操作成功");
                            }


}
